package com.ipzoe.web.controller.system;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ipzoe.common.annotation.Log;
import com.ipzoe.common.core.controller.BaseController;
import com.ipzoe.common.core.domain.AjaxResult;
import com.ipzoe.common.enums.BusinessType;
import com.ipzoe.common.utils.FirstLetterUtil;
import com.ipzoe.common.utils.poi.ExcelUtil;
import com.ipzoe.system.domain.Region;
import com.ipzoe.system.service.IRegionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 * 行政区域Controller
 * 
 * @author eric
 * @date 2020-08-26
 */
@RestController
@RequestMapping("/system/region" )
public class RegionController extends BaseController {

    @Autowired
    private IRegionService iRegionService;

    /**
     * 查询行政区域列表
     */
    @PreAuthorize("@ss.hasPermi('system:region:list')")
    @GetMapping("/list")
    public AjaxResult list(Region region) {
        List<Region> list = iRegionService.list();
        return AjaxResult.success(list);
    }

    @PreAuthorize("@ss.hasPermi('system:region:list')")
    @GetMapping("/listAllCity")
    public AjaxResult listAllCity() {
        LambdaQueryWrapper<Region> regionLqw = new LambdaQueryWrapper<>();
        regionLqw.eq(Region::getLevel, 3);
        List<Region> list = iRegionService.list(regionLqw);
        return AjaxResult.success(list);
    }

    @GetMapping("/listAllCountry")
    public AjaxResult listAllCountry() {
        LambdaQueryWrapper<Region> regionLqw = new LambdaQueryWrapper<>();
        regionLqw.eq(Region::getLevel, 1);
        List<Region> list = iRegionService.list(regionLqw);
        return AjaxResult.success(list);
    }

    /**
     * 导出行政区域列表
     */
    @PreAuthorize("@ss.hasPermi('system:region:export')" )
    @Log(title = "行政区域" , businessType = BusinessType.EXPORT)
    @GetMapping("/export" )
    public AjaxResult export(Region region) {
        LambdaQueryWrapper<Region> lqw = new LambdaQueryWrapper<Region>(region);
        List<Region> list = iRegionService.list(lqw);
        ExcelUtil<Region> util = new ExcelUtil<Region>(Region. class);
        return util.exportExcel(list, "region" );
    }

    /**
     * 获取行政区域详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:region:query')" )
    @GetMapping(value = "/{id}" )
    public AjaxResult getInfo(@PathVariable("id" ) Long id) {

        return AjaxResult.success(iRegionService.getById(id));
    }

    /**
     * 新增行政区域
     */
    @PreAuthorize("@ss.hasPermi('system:region:add')" )
    @Log(title = "行政区域" , businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Region region) {
        region.setLetter(FirstLetterUtil.getFirstLetter(region.getName()));
        return toAjax(iRegionService.save(region) ? 1 : 0);
    }

    /**
     * 修改行政区域
     */
    @PreAuthorize("@ss.hasPermi('system:region:edit')" )
    @Log(title = "行政区域" , businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Region region) {
        region.setLetter(FirstLetterUtil.getFirstLetter(region.getName()));
        return toAjax(iRegionService.updateById(region) ? 1 : 0);
    }

    /**
     * 删除行政区域
     */
    @PreAuthorize("@ss.hasPermi('system:region:remove')" )
    @Log(title = "行政区域" , businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}" )
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(iRegionService.removeByIds(Arrays.asList(ids)) ? 1 : 0);
    }
}
